package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //添加数据并获取套餐id
        setmealMapper.insert(setmeal);
        //获取菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //给数据赋值套餐id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        //将套餐-菜品信息上传
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /*
     * 套餐分页查询
     * */
    @Override
    public PageResult getPageData(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealVOPage = setmealMapper.selsctPage(setmealPageQueryDTO);

        return new PageResult(setmealVOPage.getTotal(), setmealVOPage.getResult());
    }

    /*
     * 根据id查询套餐
     * */
    @Override
    public SetmealVO getSetmealVOById(Long id) {
        //查询套餐数据
        Setmeal setmeal = setmealMapper.selsctById(id);
        //查询套餐对应菜品信息
        List<SetmealDish> setmealDishes = setmealDishMapper.selsctBySetmealId(id);
        //封装返回
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /*
     * 修改套餐
     * */
    @Transactional
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //用Setmeal对象接收数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //更新数据
        setmealMapper.update(setmeal);
        //删除原有对应菜品数据
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        //添加新的菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /*
     * 修改起售状态
     * */
    @Override
    public void updateSetmealStatus(Integer status, Long id) {
        //判断是否为启用套餐
        if (status==StatusConstant.ENABLE){
            //判断当前套餐下菜品是否都为启用
            Long number = setmealDishMapper.selsctStatusNumberById(id);
            if (number!=0){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);
    }

    /*
    * 批量删除套餐
    * */
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        //判断套餐起售状态是否是起售
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selsctById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //批量删除套餐
        setmealMapper.deleteBatch(ids);
        //批量删除套餐对应菜品信息
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /*
     * 根据分类id查询套餐
     * */
    @Override
    public List<Setmeal> getSetmealList(Setmeal setmeal) {
        return setmealMapper.select(setmeal);
    }

    /*
    * 根据套餐id查询菜品数据
    * */
    @Override
    public List<DishItemVO> getDishItemVOById(Long id) {
        //查询套餐对应菜品信息
        List<DishItemVO> dishItemVOList = setmealDishMapper.selsctDishItem(id);

        return dishItemVOList;
    }


}
